package com.force.module.webserver.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.json.JSONUtil;
import com.force.module.webserver.util.CacheUtils;
import com.force.module.webserver.util.HtmlCompressor;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/components")
public class ComponentController {

	@Autowired
	CacheUtils cacheUtils;


	// 使用注解方式开启 Http Digest 认证
	@RequestMapping(value = "/items", method = RequestMethod.GET)
	public SaResult items(String moduleName) {
		ClassPathResource classPathResource = new ClassPathResource("/static/static/components");
		List<String> strings = FileUtil.listFileNames(classPathResource.getAbsolutePath());
		return SaResult.ok(strings.toString());
	}


	// 使用注解方式开启 Http Digest 认证
	@SaIgnore
	@CrossOrigin(origins = "**", maxAge = 3600)
	@GetMapping(value = "/source/{componentType}/{fileName:.+}", produces = MediaType.TEXT_HTML_VALUE)
	public ResponseEntity<Resource> itemContentSource(@PathVariable String componentType, @PathVariable String fileName) throws MalformedURLException, FileNotFoundException {
		Validate.notBlank(componentType, "文件类型不能为空");
		Validate.notBlank(fileName, "组件名称不能为空");
		ClassPathResource classPathResource = new ClassPathResource("/static/static/" + componentType);
		Path path = Paths.get(classPathResource.getAbsolutePath(), fileName);
		Resource resource = new UrlResource(path.toUri());
		ResponseEntity<Resource> body = ResponseEntity.ok()
				.contentType(new MediaType("text", "html", StandardCharsets.UTF_8))
//				.header(HttpHeaders.CACHE_CONTROL, "max-age=36000")
//				.header("Access-Control-Allow-Origin", "*")
//				.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
//				.header("Access-Control-Allow-Headers", "*")
				.body(resource);
		String jsonStr = JSONUtil.toJsonStr(body);
		System.out.println(jsonStr);
		return body;
	}


	// 使用注解方式开启 Http Digest 认证
	@SaIgnore
	@CrossOrigin(origins = "**", maxAge = 3600)
	@GetMapping(value = "/{componentType}/{fileName:.+}", produces = MediaType.TEXT_HTML_VALUE)
	@Cacheable(value = "html", key = "#fileName")
	public String itemContentTxt(@PathVariable String componentType, @PathVariable String fileName) throws Exception {
		Validate.notBlank(componentType, "文件类型不能为空");
		Validate.notBlank(fileName, "组件名称不能为空");
		ClassPathResource classPathResource = new ClassPathResource("/static/static/" + componentType);
		Path path = Paths.get(classPathResource.getAbsolutePath(), fileName);
		Resource resource = new UrlResource(path.toUri());
		String content=StreamUtils.copyToString(resource.getInputStream(),StandardCharsets.UTF_8);

		return content;
	}



//	@GetMapping(value = "/ofaJs")
//	@SaIgnore
//	public ResponseEntity<Resource> ofaJs() throws MalformedURLException {
//
//		ClassPathResource classPathResource = new ClassPathResource("/static/static/js");
//		Path path = Paths.get(classPathResource.getAbsolutePath(), "ofa.min.js");
//		Resource resource = new UrlResource(path.toUri());
//
//		return ResponseEntity.ok()
//				.header(HttpHeaders.CACHE_CONTROL, "max-age=36000")
//				.contentType(MediaType.TEXT_PLAIN)
//				.body(resource);
//	}

}
